package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.service.CommonService;
import com.sky.utils.QiniuOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Service
@Slf4j
public class CommonServiceImpl implements CommonService {

    @Autowired
    private QiniuOssUtil qiniuOssUtil;

    @Override
    public Result<String> upload(MultipartFile file) {
        String OriginalFilename = file.getOriginalFilename();
        String extension = OriginalFilename.substring(OriginalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID() + extension;
        String msg = null;
        try {
            msg = qiniuOssUtil.upload(file.getBytes(), filename);
        } catch (IOException e) {
            log.error(e.toString());
        }
        if (msg == null){
            return Result.error(MessageConstant.UPLOAD_FAILED);
        }
        return Result.success(msg);
    }
}
